<?xml version="1.0" encoding="iso-8859-1"?>

<!-- 
  -- Please see the readme.txt file included with this distribution for 
  -- attribution and copyright information.
  -->

<root version="2.0">
	<template name="npctoplabel">
		<stringcontrol>
			<font>sheetlabelsmall</font>
			<center />
			<script>
				function updateVisibility()
					if anchor and not window[anchor[1]].isVisible() then
						setVisible(false);
					else
						setVisible(true);
					end
				end
				
				function onInit()
					if anchor then
						setAnchor("bottom", anchor[1], "top", "absolute", 2);
						setAnchor("left", anchor[1], "left", "absolute", 0);
						setAnchor("right", anchor[1], "right", "absolute", 0);
					end
				end
			</script>
		</stringcontrol>
	</template>

	<template name="npcabilityfield">
		<jpgnumberfield>
			<nodrop />
			<noreset />
			<hideonvalue>0</hideonvalue>
			<font>sheettextsmall</font>
			<frame>
				<name>modifier</name>
				<offset>5,2,5,4</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,2,5,4</offset>
			</keyeditframe>
			<script>
				function onInit()
					if super.onInit then
						super.onInit();
					end
					
					if anchor then
						setAnchor("left", anchor[1], "right", "relative", 5);
						setAnchor("top", anchor[1], "top", "absolute", 0);
					else
						setAnchor("left", "levelrole", "left", "absolute", -2);
						setAnchor("top", "levelrole", "bottom", "relative", 17);
					end
					
					setAnchoredWidth(20);
					setAnchoredHeight(18);
					
					if NodeManager.isReadOnly(getDatabaseNode()) then
						setFont("chatfont");
					elseif getValue() == 0 then
						setValue(10);
					end
				end

				function onDrag(button, x, y, draginfo)
					if dragging then
						return true;
					end
					
					local rActor, rAbilityCheck = CombatCommon.getAbilityCheckRollStructures("npc", window.getDatabaseNode(), self.target[1]);
					local ability_name, ability_dice, ability_mod = RulesManager.buildAbilityCheckRoll(rActor, rAbilityCheck);
					dragging = RulesManager.dragAction(draginfo, "ability", ability_mod, ability_name, rActor, ability_dice);
					
					return true;
				end
					
				function onDragEnd(draginfo)
					dragging = false;
				end

				function onDoubleClick(x, y)	
					local rActor, rAbilityCheck = CombatCommon.getAbilityCheckRollStructures("npc", window.getDatabaseNode(), self.target[1]);
					local ability_name, ability_dice, ability_mod = RulesManager.buildAbilityCheckRoll(rActor, rAbilityCheck);
					return RulesManager.dclkAction("ability", ability_mod, ability_name, rActor, nil, ability_dice);
				end
			</script>
		</jpgnumberfield>
	</template>

	<template name="npcdefensefield">
		<jpgnumberfield>
			<nodrop />
			<noreset />
			<font>sheettextsmall</font>
			<frame>
				<name>modifier</name>
				<offset>5,2,5,4</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,2,5,4</offset>
			</keyeditframe>
			<script>
				function onInit()
					if super.onInit then
						super.onInit();
					end
					
					if anchor then
						setAnchor("left", anchor[1], "right", "relative", 5);
						setAnchor("top", anchor[1], "top", "absolute", 0);
					else
						setAnchor("left", baseanchor[1], "left", "absolute", 0);
						setAnchor("top", baseanchor[1], "bottom", "relative", 17);
					end
					
					setAnchoredWidth(34);
					setAnchoredHeight(18);
					
					if NodeManager.isReadOnly(getDatabaseNode()) then
						setFont("chatfont");
					elseif getValue() == 0 then
						setValue(10);
					end
				end
			</script>
		</jpgnumberfield>
	</template>

	<template name="npcpowerstring">
		<stringfield>
			<font>sheettextsmall</font>
			<frame>
				<name>modifier</name>
				<offset>6,5,10,5</offset>
			</frame>
			<nodrag />
			<script>
				function onInit()
					if NodeManager.isReadOnly(getDatabaseNode()) then
						if getValue() == "" then
							setVisible(false);
						end
					end
				end
				
				function onEnter()
					if Input.isShiftPressed() then
						if window.windowlist and window.windowlist.onEnter then
							window.windowlist.onEnter();
						end
						return true;
					else
						--[[ RETURNING FALSE DOES NOT PERFORM DEFAULT BEHAVIOR AS DOCUMENTED. ]]
						--[[ return false; ]]
					end
				end
			</script>
		</stringfield>
	</template>

	<template name="npcpowerstringmultiline">
		<stringfield>
			<font>sheettextsmall</font>
			<multilinespacing>15</multilinespacing>
			<frame>
				<name>modifier</name>
				<offset>6,5,5,5</offset>
			</frame>
			<nodrag />
			<script>
				function onInit()
					if NodeManager.isReadOnly(getDatabaseNode()) then
						if getValue() == "" then
							setVisible(false);
						end
					end
				end
				
				function onEnter()
					if Input.isShiftPressed() then
						if window.windowlist and window.windowlist.onEnter then
							window.windowlist.onEnter();
						end
						return true;
					else
						--[[ RETURNING FALSE DOES NOT PERFORM DEFAULT BEHAVIOR AS DOCUMENTED. ]]
						--[[ return false; ]]

						local nCursor = getCursorPosition();
						local nSelection = getSelectionPosition();
						if nSelection >= nCursor then
							local sValue = getValue();
							sValue = string.sub(sValue, 1, nCursor - 1) .. "\r" .. string.sub(sValue, nSelection);
							setValue(sValue);
							setCursorPosition(nCursor + 1);
						else
							local sValue = getValue();
							sValue = string.sub(sValue, 1, nSelection - 1) .. "\r" .. string.sub(sValue, nCursor);
							setValue(sValue);
							setCursorPosition(nSelection + 1);
						end
						setSelectionPosition(0);
						return true;
					end
				end
			</script>
		</stringfield>
	</template>

	<template name="justified_npcpowerstring">
		<npcpowerstring>
			<script>
				function onInit()
					if anchor then
						setAnchor("left", anchor[1], "right", "relative", 7);
						setAnchor("top", anchor[1], "top", "absolute", 0);
					else
						setAnchor("left", "", "left", "absolute", 12);
						setAnchor("top", "recharge", "bottom", "relative", 4);
					end
					
					setAnchoredHeight(18);

					if NodeManager.isReadOnly(getDatabaseNode()) then
						if getValue() == "" then
							setVisible(false);
						end
					end
				end
			</script>
		</npcpowerstring>
	</template>
</root>
